<!DOCTYPE html>
<html>
<head>
<script src="../../../resources/js-test.js"></script>
<script src="../resources/common.js"></script>
</head>
<body>
<p id="description"></p>
<div id="console"></div>

<script>
description("Test importing a JWK key for HMAC.");

jsTestIsAsync = true;

var nonExtractable = false;
var extractable = true;

var hmacKey = {
    "kty": "oct",
    "alg": "HS256",
    "use": "sig",
    "ext": false,
    "k": "ahjkn-_387fgnsibf23qsvahjkn-_387fgnsibf23qs"
};

debug("Importing a key...\n");
crypto.subtle.importKey("jwk", hmacKey, {name: "HMAC", hash: {name: "SHA-256"}}, nonExtractable, ["sign", "verify"]).then(function(result) {
    key = result;

    shouldBe("key.type", "'secret'");
    shouldBe("key.extractable", "false");
    shouldBe("key.algorithm.name", "'HMAC'");
    shouldBe("key.algorithm.length", "256");
    shouldBe("key.usages", '["sign", "verify"]');

    debug("\nUsing the key to sign message 'foo'...");
    return crypto.subtle.sign(key.algorithm, key, asciiToUint8Array('foo'));
}).then(function(result) {
    signature = result;
    shouldBe("bytesToHexString(new Uint8Array(signature))", "'e03736fe098892b2a2da77812431f7c014d32e2fd69f3bcff883ac923a8fa2da'");

    debug("\nVerifying the signature...");
    return crypto.subtle.verify(key.algorithm, key, result, asciiToUint8Array('foo'));
}).then(function(result) {
    verificationResult = result;
    shouldBe("verificationResult", "true");
}).then(finishJSTest, failAndFinishJSTest);
</script>

</body>
</html>
